package fatal

import (
	"os"
	"fmt"
	"reflect"
	"kumachan/standalone/util/richtext"
)


type Exception interface {
	Message() richtext.Block
}

type GoErrorException struct {
	error
}
func (e GoErrorException) Message() richtext.Block {
	var b richtext.Block
	b.WriteLine(e.Error(), richtext.TAG_ERR_NORMAL)
	return b
}

func Crash(e Exception, title string) {
	var e_type = reflect.TypeOf(e).String()
	var e_msg = e.Message()
	var msg richtext.Block
	msg.WriteLine(fmt.Sprintf("*** %s ***", title), richtext.TAG_EM)
	msg.WriteLine(fmt.Sprintf("exception: %s", e_type))
	msg.Append(e_msg)
	var msg_text = msg.RenderConsole(richtext.RenderOptionsConsole {
		AnsiPalette: richtext.DefaultPalette(),
	})
	fmt.Fprintf(os.Stderr, "%s\n", msg_text)
	os.Exit(126)
}

func CrashGoError(err error, title string) {
	Crash(GoErrorException { error: err }, title)
}

func BadArgs(err error, help string) {
	fmt.Fprintf(os.Stderr,
		"bad command line arguments:\n\t%s", err.Error())
	fmt.Fprintf(os.Stderr,
		"%s\n", help)
	os.Exit(127)
}


